import { type ShippingTemplateType } from "./../src/api/goods/shipping-template";
import { defineMock } from "./base";

const records: ShippingTemplateType[] = [
  {
    id: 1,
    created_at: "2023-05-01T00:00:00Z",
    updated_at: "2023-05-01T00:00:00Z",
    name: "模板1",
    billing_method: "quantity",
    free_shipping: true,
    non_delivery: false,
    sort: 0,
  },
  {
    id: 2,
    created_at: "2023-05-01T00:00:00Z",
    updated_at: "2023-05-01T00:00:00Z",
    name: "模板2",
    billing_method: "volume",
    free_shipping: false,
    non_delivery: true,
    sort: 1,
  },
  {
    id: 3,
    created_at: "2023-05-01T00:00:00Z",
    updated_at: "2023-05-01T00:00:00Z",
    name: "模板3",
    billing_method: "weight",
    free_shipping: true,
    non_delivery: false,
    sort: 2,
  },
];

export default defineMock([
  {
    url: "/shipping/template/",
    method: ["GET"],
    body({ query }) {
      const { page, size } = query;
      return {
        records: records.slice((page - 1) * size, page * size),
        total_count: records.length,
        total_page: Math.ceil(records.length / size),
      };
    },
  },
  {
    url: "/shipping/template/",
    method: ["POST"],
    body() {
      const item: ShippingTemplateType = {
        id: records.length + 1,
        created_at: new Date().toISOString(),
        updated_at: new Date().toISOString(),
        name: "模板" + (records.length + 1),
        billing_method: "quantity",
        free_shipping: true,
        non_delivery: false,
        sort: records.length,
      };
      records.push(item);
      return item;
    },
  },
  {
    url: "/shipping/template/:id",
    method: ["PUT"],
    body({ params, body }) {
      const index = records.findIndex((item) => item.id === params.id);
      records[index] = {
        ...records[index],
        ...body,
      };
      return records[index];
    },
  },
  {
    url: "/shipping/template/:id",
    method: ["DELETE"],
    body({ params }) {
      const index = records.findIndex((item) => item.id === params.id);
      records.splice(index, 1);
      return null;
    },
  },
]);
